﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.Company
{
   public static class ResumeFavoritesManager
    {
       /// <summary>
       /// 收藏简历
       /// </summary>
       /// <param name="_company"></param>
       /// <param name="_resumeID"></param>
       /// <param name="_jobID"></param>
       /// <returns></returns>
       public static bool AddToFavorites(string _company, string _resumeID, Guid _jobID)
       {
           #region 入口检测

           if (string.IsNullOrEmpty(_company)) throw new Exception("企业名称不能为空");
           if (string.IsNullOrEmpty(_resumeID)) throw new Exception("简历ID不能为空");
           if (_jobID == Guid.Empty) throw new Exception("工作ID不能为空");

           #endregion

           var _resumeFav = DBHelper.Get<ResumeFavorites>(rf => rf.Company == _company && rf.ResumeID == _resumeID && rf.JobID == _jobID,
               "获取收藏简历出错:");

           if (_resumeFav == null)
           {
               using (var _adc = DBHelper.GetDataContext())
               {
                   ResumeFavorites _rf = new ResumeFavorites();

                   _rf.Company = _company;
                   _rf.ResumeID = _resumeID;
                   _rf.JobID = _jobID;
                   _rf.Date = DateTime.Now;

                   _adc.ResumeFavorites.InsertOnSubmit(_rf);
                   _adc.SubmitChanges();
               }
           }
           return true;
       }

       /// <summary>
       /// 获取简历收藏信息
       /// </summary>
       /// <param name="_company"></param>
       /// <param name="_resumeID"></param>
       /// <param name="_jobID"></param>
       /// <returns></returns>
       public static ResumeFavorites GetFavorite(string _company, string _resumeID, Guid _jobID)
       {
           return DBHelper.Get<ResumeFavorites>(rf => rf.Company == _company && rf.ResumeID == _resumeID && rf.JobID == _jobID, "");
       }
       /// <summary>
       /// 获取简历收藏列表
       /// </summary>
       /// <param name="_company"></param>
       /// <returns></returns>
       public static List<ResumeFavorites> GetFavorites(string _company)
       {
           return DBHelper.GetAll<ResumeFavorites>(rf=>rf.Company==_company,"获取简历收藏列表出错:");
       }
       /// <summary>
       /// 删除已收藏的简历
       /// </summary>
       /// <param name="_favoriteID"></param>
       /// <returns></returns>
       public static bool DeleteAFavorite(int _favoriteID)
       {
           using (var _adc = DBHelper.GetDataContext())
           {
               var _rf = _adc.ResumeFavorites.Where(f => f.ID == _favoriteID).FirstOrDefault();
               if (_rf != null)
               {
                   _adc.ResumeFavorites.DeleteOnSubmit(_rf);
                   _adc.SubmitChanges();
               }
           }
           return true;
       }
    }
}
